package com.ruoyi.project.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.SysType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.SysCityMapper;
import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.service.ISysCityService;

/**
 * 城市Service业务层处理
 * 
 * @author jjp
 * @date 2020-04-09
 */
@Service
public class SysCityServiceImpl implements ISysCityService 
{
    @Autowired
    private SysCityMapper sysCityMapper;

    /**
     * 查询城市
     * 
     * @param cityId 城市ID
     * @return 城市
     */
    @Override
    public SysCity selectSysCityById(Long cityId)
    {
        return sysCityMapper.selectSysCityById(cityId);
    }

    /**
     * 查询城市列表
     * 
     * @param sysCity 城市
     * @return 城市
     */
    @Override
    public List<SysCity> selectSysCityList(SysCity sysCity)
    {
        return sysCityMapper.selectSysCityList(sysCity);
    }

    @Override
    public List<SysCity> getByCityName(SysCity sysCity) {
        return sysCityMapper.getByCityName(sysCity);
    }

    /**
     * 新增城市
     * 
     * @param sysCity 城市
     * @return 结果
     */
    @Override
    public int insertSysCity(SysCity sysCity)
    {
        sysCity.setCreateTime(DateUtils.getNowDate());
        return sysCityMapper.insertSysCity(sysCity);
    }

    @Override
    public int insetCityParentId(SysCity sysCity) {
        return sysCityMapper.insetCityParentId(sysCity);
    }

    /**
     * 修改城市
     * 
     * @param sysCity 城市
     * @return 结果
     */
    @Override
    public int updateSysCity(SysCity sysCity)
    {
        sysCity.setUpdateTime(DateUtils.getNowDate());
        return sysCityMapper.updateSysCity(sysCity);
    }

    /**
     * 批量删除城市
     * 
     * @param cityIds 需要删除的城市ID
     * @return 结果
     */
    @Override
    public int deleteSysCityByIds(Long[] cityIds)
    {
        return sysCityMapper.deleteSysCityByIds(cityIds);
    }

    /**
     * 删除城市信息
     * 
     * @param cityId 城市ID
     * @return 结果
     */
    @Override
    public int deleteSysCityById(Long cityId)
    {
        return sysCityMapper.deleteSysCityById(cityId);
    }

    @Override
    public List<SysCity> QueryCityService(Long cityId)
    {
        return sysCityMapper.QueryCityService(cityId);
    }

    @Override
    public List<SysCity> selectSysParentCityId(Long level) {
        return sysCityMapper.selectSysParentCityId(level);
    }


    public List<SysCity> buildTypeTree(List<SysCity> sysCities) {
        List<Long> cityIds = new ArrayList<>();
        for (SysCity sysCity : sysCities) {
            cityIds.add(sysCity.getCityId());
        }

        List<Long> parentCityIds = new ArrayList<>();
        for (SysCity sysCity : sysCities) {
            if (!cityIds.contains(sysCity.getParentCityId())){
                parentCityIds.add(sysCity.getCityId());
            }
        }
        List<SysCity> returnList = new ArrayList<SysCity>();
        for (Iterator<SysCity> iterator = sysCities.iterator(); iterator.hasNext(); ) {
            SysCity t = (SysCity) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (parentCityIds.contains(t.getCityId())) {
                recursionFn(sysCities, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = sysCities;
        }
        return returnList;
    }

    @Override
    public List<SysCity> queryParentName() {
        return sysCityMapper.queryParentName();
    }

    @Override
    public List<SysCity> queryChirdrenName(SysCity sysCity) {
        return sysCityMapper.queryChirdrenName(sysCity);
    }

    @Override
    public List<SysCity> queryByCityName(SysCity sysCity) {
        return sysCityMapper.queryByCityName(sysCity);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysCity> list, SysCity t) {
        // 得到子节点列表
        List<SysCity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysCity tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysCity> it = childList.iterator();
                while (it.hasNext()) {
                    SysCity n = (SysCity) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysCity> getChildList(List<SysCity> list, SysCity t) {
        List<SysCity> tlist = new ArrayList<SysCity>();
        Iterator<SysCity> it = list.iterator();
        while (it.hasNext()) {
            SysCity n = (SysCity) it.next();
            if (null != n.getParentCityId() && (n.getParentCityId().longValue() == t.getCityId().longValue())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysCity> list, SysCity t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }


}
